home *** CD-ROM | disk | FTP | other *** search
- Path: nntp-server.caltech.edu!news
- From: Xuhua Li <xuhua@cco.caltech.edu>
- Newsgroups: comp.lang.c++
- Subject: C++ newbie asks for help
- Date: Thu, 14 Mar 1996 13:08:24 -0800
- Organization: California Institute of Technology, Pasadena
- Message-ID: <31488AC8.1557@cco.caltech.edu>
- NNTP-Posting-Host: xuhua-ppp.caltech.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=gb2312
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
-
- The following C++ programs from Stephen Prata's book <<C++ Primer Plus>>
- does not work properly compiled by Microsoft C++ 4.0 under Window 95.
- Can anybody help me out there?
- Program #1:
- // waiting.cpp -- using clock() in a time-delay loop
- #include <iostream.h>
- #include <time.h> // describes clock() function, clock_t type
- int main(void)
- {
- cout << "Enter the delay time, in seconds: ";
- float secs; cin >> secs;
- clock_t delay = secs * CLOCKS_PER_SEC;
- // convert to clock ticks
- cout << "starting\a\n";
- clock_t start = clock();
- while (clock() - start < delay )
- // wait until time elapses
- ;
- cout << "done \a\n";
- return 0;
- }
- After I compile the program using Visual C++ 4.0's command line compiler
- cl, I run the .exe file waiting.exe. It is supposed to work in the
- following way:
- Enter the delay time: (I input 10 here)
- starting\a
- (The computer waits for 10 sec.)
- done\a.
- But it works in the following way indeed:
- Enter the delay time: (I input 10)
- (The conputer wait for 10 secs)
- starting\a
- done\a.
- I do not know what happened!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
- Program #2
- // textin3.cpp -- reading chars to end of file
- #include <iostream.h>
- int main(void)
- {
- char ch;
- int count = 0;
-
- while (cin.get(ch)) // cin.get(ch) is 0 on EOF
- {
- cout << ch;
- count++;
- }
- cout << count << " characters read\n";
- return 0;
- }
- After I input the simulated EOF CTRL-Z, the problem quits without
- reporting the count.
-
- They are very simple program but work strange, can you tell me what
- is wrong with them.
- Your assistance will be highly appriciated.
-